﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core;
using ZHDJ.Data.BLL;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Common;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.Data.Models.Org;
using ZHDJ.WebPage;

namespace ZHDJ.Web.Portal
{
    public partial class BranchIndex1 : PortalPage
    {
        public const string PhotoWallCacheKey = "PARTY_BranchIndex_PhotoWall_DATASOURCE";
        protected string OrgDesc = string.Empty;

        public string BranchID
        {
            get
            {
                return this.Request["BranchID"] ?? "";
            }
        }

        public string BranchName { get; set; }

        public string BranchType { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        #region 数据操作
        private void BindData()
        {

        }
        private void LoadData()
        {
            LoadBranchInfo();
            //组织动态图
            //rpBranchActivitieImg.DataSource = PartyBranchActivitie.GetBranchActivitieTitle(this.BranchID, "limit 0,6");
            //rpBranchActivitieImg.DataBind();

            //组织动态-头条
            rpBranchActivitie1.DataSource = PartyBranchActivitie.GetBranchActivitieTitleNoFlags(this.BranchID, "limit 0,1").Tables[0];
            rpBranchActivitie1.DataBind();

            //组织动态
            rpBranchActivitie.DataSource = PartyBranchActivitie.GetBranchActivitieTitleNoFlags(this.BranchID, "limit 1,8").Tables[0];
            rpBranchActivitie.DataBind();

            //rpBranchActivitieLi.DataSource = PartyBranchActivitie.GetBranchActivitieTitle(this.BranchID, "limit 0,6");
            //rpBranchActivitieLi.DataBind();

            //rpBranchActivitieContent.DataSource = PartyBranchActivitie.GetBranchActivitieTitle(this.BranchID, "limit 0,6");
            //rpBranchActivitieContent.DataBind();

            //志愿活动
            rpVolunteerActivity.DataSource = PartyVolunteerActivitie.GetActiviteTitle(this.BranchID, "limit 0,7").Tables[0];
            rpVolunteerActivity.DataBind();

            //志愿心语
            rpQuotation.DataSource = PartyVolunteerQuotation.GetQuotationTitle(this.BranchID, "limit 0,7").Tables[0];
            rpQuotation.DataBind();

            //党务公开
            //rpPartyPublic.DataSource = PartyArticle.GetPartyPublicData(this.BranchID);
            //rpPartyPublic.DataBind();

            ////村务公开
            //rpVillagePublic.DataSource = PartyArticle.GetVillagePublicData(this.BranchID);
            //rpVillagePublic.DataBind();

            ////财务公开
            //rpFinancePublic.DataSource = PartyArticle.GetFinancePublicData(this.BranchID);
            //rpFinancePublic.DataBind();

            #region 三务公开
            //党务公开
            rpPartyAffairs.DataSource = PartyArticle.GetArticleTitle(this.BranchID, SysEnum.ArticleModule.ThreeAffairOpen.ToString(), SysEnum.ArticleAffairOpen.PartyAffairs.ToString(), "limit 0,9").Tables[0];
            rpPartyAffairs.DataBind();

            //村务公开
            rpVillageAffairs.DataSource = PartyArticle.GetArticleTitle(this.BranchID, SysEnum.ArticleModule.ThreeAffairOpen.ToString(), SysEnum.ArticleAffairOpen.VillageAffairs.ToString(), "limit 0,9").Tables[0];
            rpVillageAffairs.DataBind();

            //财务公开
            rpFinance.DataSource = PartyArticle.GetArticleTitle(this.BranchID, SysEnum.ArticleModule.ThreeAffairOpen.ToString(), SysEnum.ArticleAffairOpen.Finance.ToString(), "limit 0,9").Tables[0];
            rpFinance.DataBind();

            //涉农政策
            rpAgriculturalPolicy.DataSource = PartyArticle.GetArticleTitle(this.BranchID, SysEnum.ArticleModule.ThreeAffairOpen.ToString(), SysEnum.ArticleAffairOpen.AgriculturalPolicy.ToString(), "limit 0,9").Tables[0];
            rpAgriculturalPolicy.DataBind();

            //党务政务直通车
            rpPartyRemittance.DataSource = PartyArticle.GetArticleTitle(this.BranchID, SysEnum.ArticleModule.ThreeAffairOpen.ToString(), SysEnum.ArticleAffairOpen.PartyRemittance.ToString(), "limit 0,9").Tables[0];
            rpPartyRemittance.DataBind();
            #endregion

            //DataTable dt = (DataTable)CacheHelper.GetCache(PhotoWallCacheKey);
            DataTable dt = PartyPhotoWallImage.GetBranchPhotoWallList(10, this.BranchID, "");
            this.rptHonorHall.DataSource = dt;
            this.rptHonorHall.DataBind();

            //发展党员
            rpPublicity.DataSource = partyjoinpublicity.GetListDataByBranchID(0, 7, BranchID).Tables[0];
            rpPublicity.DataBind();

            //组织关系转接
            rpTransfer.DataSource = PartyRelationnshipPublicity.GetListDataByBranchID(0, 7, BranchID).Tables[0];
            rpTransfer.DataBind();

            //党员排名
            rpRankMember.DataSource = PartyScoreHelper.GetScoreRankMemberByBranchId(0, 4, BranchID).Tables[0];
            rpRankMember.DataBind();

            ////组织排名
            //rpRankBranch.DataSource = PartyScoreHelper.GetScoreRankBranch(0, 5, BranchID, "").Tables[0];
            //rpRankBranch.DataBind();
            //程序临时使用测试数据方法
            rpRankBranch.DataSource = PartyScoreHelper.GetScoreRankBranchTest(0, 4, BranchID, "").Tables[0];
            rpRankBranch.DataBind();


            //组织生活排名
            rpRankBranchActivity.DataSource = PartyScoreHelper.GetAllRankBranchActivities(0, 5, BranchID, "").Tables[0];
            rpRankBranchActivity.DataBind();

            //组织集中学习排名
            rptRankBranchMassLearning.DataSource = PartyScoreHelper.GetAllRankBranchMassLearning(0, 5, BranchID, "").Tables[0];
            rptRankBranchMassLearning.DataBind();

            //个人学习排名
            rptRankMemberAll.DataSource = PartyScoreHelper.GetAllRankMember(0, 5, BranchID, "").Tables[0];
            rptRankMemberAll.DataBind();


            //党费
            rpMemberCost.DataSource = PartyMemberCost.GetMemberCost(0, 40, BranchID).Tables[0];
            rpMemberCost.DataBind();


            //三会一课、活动记录
            rpThreeLessons.DataSource = PartyMeetingLecture.GetGridData1(0, 40, BranchID).Tables[0];
            rpThreeLessons.DataBind();

            //三会一课、活动预告
            rpThreeLessons1.DataSource = PartyMeetingLecture.GetGridData2(0, 40, BranchID).Tables[0];
            rpThreeLessons1.DataBind();

            //先进典型
            rpMassHonour.DataSource = PartyArticle.GetMassHonourArticle(0, 40, "4", "", this.BranchID).Tables[0];
            rpMassHonour.DataBind();

            //心得体会
            //rpExperience.DataSource = PartyArticle.GetPartySoundArticle(0, 5, BranchID, "", "Experience", "");
            //rpExperience.DataBind();

            //思想汇报
            //rpThoughtReport.DataSource = PartyArticle.GetPartySoundArticle(0, 5, BranchID, "", "ThoughtReport", "");
            //rpThoughtReport.DataBind();

            //发展党员公示
            //rpPartyJoinPublicity.DataSource = partyjoinpublicity.GetListDataByBranchID(0, 9, this.BranchID).Tables[0];
            //rpPartyJoinPublicity.DataBind();
            //组织关系转移公示
            //rpPartyRelationshipPublicity.DataSource = PartyRelationnshipPublicity.GetListDataByBranchID(0, 5, this.BranchID).Tables[0];
            //rpPartyJoinPublicity.DataBind();

            //心得体会
            rptExperience.DataSource = PartyArticle.GetPartySoundArticle(0, 7, this.BranchID, "", "Experience", "", "").Tables[0];
            rptExperience.DataBind();

            //思想汇报
            rptThoughtReport.DataSource = PartyArticle.GetPartySoundArticle(0, 7, this.BranchID, "", "ThoughtReport", "", "").Tables[0];
            rptThoughtReport.DataBind();

            //上级精神
            rpMassSuperiorSpirit.DataSource = PartyArticle.GetMassLineSuperiorSpirit(0, 10, "", "MassLine", "SuperiorSpirit");
            rpMassSuperiorSpirit.DataBind();

        }

        public void LoadBranchInfo()
        {
            var branch = DB.PartyOrgBranchs.Find(BranchID);

            if (branch == null)
            {
                RedirectTo("Login", "~/default.aspx");
            }
            this.litBranchName.Text = string.Format(" <a href='../BranchIndex.aspx?BranchID={0}' class='abranchname'>{1}</a>", BranchID, branch.BranchName);
            this.litBranchName.Text = branch.BranchName;
            //litMemberCount.Text = DB.PartyOrgMembers.Count(a => a.runSate == "0" && a.BranchID == BranchID).ToString();
            //if (branch.BranchMark == "3" || branch.BranchMark == "5")
            //{
            //    BranchScore.Visible = false;
            //}
            if (!string.IsNullOrEmpty(branch.ImageUrl))
            {
                ImageAuto.ImgUrl = UpFileBusiness.GetFileUrl(branch.ImageUrl);
            }
            if (string.IsNullOrEmpty(branch.Description))
            {
                lbOrgDesc.Text = branch.BranchName;
                lbOrgdesc2.Text = branch.BranchName;
                OrgDesc = branch.BranchName;
            }
            else
            {
                lbOrgDesc.Text = CutLongTitle(branch.Description, 120);
                lbOrgdesc2.Text = CutLongTitle(branch.Description, 120);
                OrgDesc = branch.Description;
            }

            var dt = PartyScoreHelper.QueryBranchScore(BranchID);
            if (dt.Rows.Count > 0)
            {
                this.litRank.Text = Convert.ToString(dt.Rows[0]["Rank"]);
                this.litScore.Text = Convert.ToString(dt.Rows[0]["Score"]);
            }
            else
            {
                this.litRank.Text = "没有排名";
                this.litScore.Text = "0";
            }
            BranchName = branch.BranchName;
        }
        #endregion

        [WebMethod]
        public static string IsHasChildrenBranch(string branchId)
        {
            if (PartyOrgBranch.IsHasChildrenBranch(branchId))
            { return "true"; }
            else if (PartyOrgBranch.IsOfficialPartyBranch(branchId))
            {
                return "true";
            }
            else
            { return "false"; }
        }


        [WebMethod]
        public static string IsStreetOrViliage(string branchId)
        {
            if (PartyOrgBranch.IsStreetOrViliage(branchId))
            { return "true"; }
            else
            { return "false"; }
        }

        /// <summary>
        /// 截取标题
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string CutLongTitle(object obj, int length)
        {
            string title = Convert.ToString(obj);
            if (title.Length > length)
            {
                title = title.Substring(0, length) + "...";
            }
            return title;
        }
    }
}